package com.bobochang.warehouse.controller;

import com.bobochang.warehouse.annotation.BusLog;
import com.bobochang.warehouse.dto.CollectSettleAccountsDto;
import com.bobochang.warehouse.dto.ConstructionDetailsDto;
import com.bobochang.warehouse.dto.NewConstructionDetailsDto;
import com.bobochang.warehouse.entity.ConstructionAndContract;
import com.bobochang.warehouse.entity.ConstructionDetails;
import com.bobochang.warehouse.entity.Contract;
import com.bobochang.warehouse.entity.Result;
import com.bobochang.warehouse.page.Page;
import com.bobochang.warehouse.service.ConstructionAndContractService;
import com.bobochang.warehouse.service.ConstructionDetailsService;
import com.bobochang.warehouse.service.ConstructionTypeService;
import com.bobochang.warehouse.service.DailyConstructionService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.constructor.Construct;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

@RequestMapping("/construction")
@RestController
@Transactional
@BusLog(name = "施工明细管理")
public class ConstructionController {
    @Resource
    private ConstructionAndContractService constructionAndContractService;
    @Resource
    private ConstructionTypeService constructionTypeService;
    @Resource
    private ConstructionDetailsService constructionDetailsService;
    @Resource
    private DailyConstructionService dailyConstructionService;

    /**
     * 添加施工明细
     */
    @PostMapping("/add")
    public Result addConstruction(@RequestBody NewConstructionDetailsDto newConstructionDetailsDto){
           return dailyConstructionService.add(newConstructionDetailsDto);
    }
    /**
     * 分页查询施工明细
     * @param page
     * @param constructionDetails
     * @return
     */
    @GetMapping("/construction-list")
    public Result getConstructionList(Page page, ConstructionDetailsDto constructionDetails)
    {
        page = dailyConstructionService.queryConstructionPage(page,constructionDetails);
        return Result.ok(page);
    }

    /**
     * 汇总表，单合同，按位置、类型分类
     */
    @GetMapping("/collectProject")
    public Result getConstructionCollect(Contract contract)
    {
        if(contract.getContractId() == null && contract.getContractName() == null){
            return Result.err(Result.CODE_ERR_BUSINESS, "输入为空");
        }
        //return Result.ok(dailyConstructionService.findCollect(contract));
        return Result.ok(dailyConstructionService.findCollectList(contract));
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result updateConstruction(@RequestBody NewConstructionDetailsDto newConstructionDetailsDto){
        return dailyConstructionService.updateConstruction(newConstructionDetailsDto);
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete/{constructionId}")
    public Result delete(@PathVariable Integer constructionId){
        return dailyConstructionService.delete(constructionId);
    }

    @PostMapping("/collectProduct")
    public Result getProductCollect(@RequestBody Map<String,String> params)
    {
        Date date = null;
        if(params.get("date") != null){
            try {
                date = new SimpleDateFormat("yyyy-MM").parse(params.get("date"));
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }

        return Result.ok(dailyConstructionService.collectProduct(params.get("contractId"),date));
    }

    @PostMapping("/collectSettleAccounts")
    public Result getCollectSettleAccounts(@RequestBody Map<String,String> params)
    {
        List<CollectSettleAccountsDto> collectSettleAccountsDtoList = dailyConstructionService.collectSettleAccounts(params.get("contractId"),params.get("startTime"),params.get("endTime"));
        if(collectSettleAccountsDtoList == null){
            return Result.err(Result.CODE_ERR_BUSINESS, "请输入合同名");
        }
        return Result.ok(collectSettleAccountsDtoList);
    }
}
